<script setup>
import { ref, computed, onMounted } from 'vue'
import Home from './components/Home.vue'
import Category from './components/Category.vue'
import Article from './components/Article.vue'
import UserInfo from './components/UserInfo.vue'
import ResetPassword from './components/ResetPassword.vue'
import Login from './components/Login.vue'
import Register from './components/Register.vue'
import {
  House, 
  Menu, 
  Document, 
  User, 
  Key, 
  ArrowRight 
} from '@element-plus/icons-vue'
import { ElMessage, ElContainer, ElAside, ElHeader, ElMain, ElFooter, ElMenu, ElMenuItem, ElSubMenu, ElDropdown, ElDropdownMenu, ElDropdownItem, ElAvatar } from 'element-plus'
import useUserStore from './store/user.js'

// 获取用户 store
const userStore = useUserStore()

// 当前激活的菜单
const activeMenu = ref('home')
// 下拉菜单是否显示
const dropdownVisible = ref(false)

// 根据当前 URL 路径返回对应的组件
const currentView = computed(() => {
  const path = window.location.pathname
  
  // 如果未登录，显示登录或注册页面
  if (!userStore.isLoggedIn) {
    if (path === '/register') {
      return Register
    }
    return Login
  }
  
  // 已登录的情况下，根据激活的菜单返回对应的组件
  switch (activeMenu.value) {
    case 'home':
      return Home
    case 'category':
      return Category
    case 'article':
      return Article
    case 'userInfo':
      return UserInfo
    case 'resetPassword':
      return ResetPassword
    default:
      return Home
  }
})

// 组件挂载时检查登录状态
onMounted(() => {
  if (!userStore.isLoggedIn && window.location.pathname !== '/login' && window.location.pathname !== '/register') {
    window.location.href = '/login'
  }
})

// 处理菜单点击
const handleMenuClick = (index) => {
  activeMenu.value = index
}

// 处理用户中心点击
const handleUserCenterClick = () => {
  activeMenu.value = 'userInfo'
  dropdownVisible.value = false
}

// 处理修改密码点击
const handleChangePasswordClick = () => {
  activeMenu.value = 'resetPassword'
  dropdownVisible.value = false
}

// 处理退出登录点击
const handleLogoutClick = () => {
  userStore.logout()
  ElMessage({ 
    message: '已退出登录', 
    type: 'success',
    duration: 2000
  })
  dropdownVisible.value = false
  // 退出登录后跳转到登录页面
  window.location.href = '/login'
}
</script>

<template>
  <!-- 未登录状态：只显示登录或注册页面 -->
  <div v-if="!userStore.isLoggedIn" class="auth-container">
    <component :is="currentView" />
  </div>
  
  <!-- 已登录状态：显示完整的系统布局 -->
  <el-layout v-else class="app-container">
    <!-- 左侧侧边栏 -->
    <el-aside width="240px" class="aside-container">
      <div class="logo-container">
        <h1 class="logo-text">大事件-文章管理系统</h1>
      </div>
      <el-menu
        default-active="home"
        class="sidebar-menu"
        @select="handleMenuClick"
        background-color="#2c3e50"
        text-color="#ecf0f1"
        active-text-color="#3498db"
        unique-opened
      >
        <el-menu-item index="home" :icon="House">
          <span>首页</span>
        </el-menu-item>
        <el-menu-item index="category" :icon="Menu">
          <span>分类管理</span>
        </el-menu-item>
        <el-menu-item index="article" :icon="Document">
          <span>文章管理</span>
        </el-menu-item>
        <el-sub-menu index="userCenter" :icon="User">
          <template #title>
            <span>用户中心</span>
          </template>
          <el-menu-item index="userInfo">
            <span>基本信息</span>
          </el-menu-item>
          <el-menu-item index="resetPassword" :icon="Key">
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>

    <!-- 右侧主区域 -->
    <el-container style="width: 100%;">
      <!-- 顶部信息栏 -->
      <el-header class="header-container">
        <div class="header-left">
          <span class="user-info">当前用户：（{{ userStore.username }}）</span>
        </div>
        <div class="header-right">
          <el-dropdown v-model:visible="dropdownVisible" placement="bottom">
            <div class="user-avatar" @click="dropdownVisible = !dropdownVisible">
              <el-avatar :size="40" class="avatar">
                {{ userStore.username.substring(0, 1) }}
              </el-avatar>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleUserCenterClick">
                  <el-icon><User /></el-icon>
                  <span>用户中心</span>
                </el-dropdown-item>
                <el-dropdown-item @click="handleChangePasswordClick">
                  <el-icon><Key /></el-icon>
                  <span>修改密码</span>
                </el-dropdown-item>
                <el-dropdown-item divided @click="handleLogoutClick">
                  <el-icon><ArrowRight /></el-icon>
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 核心展示区域 -->
      <el-main class="main-container">
        <component :is="currentView" />
      </el-main>

      <!-- 底部版权信息 -->
      <el-footer class="footer-container">
        <span>© 2024 大事件-文章管理系统 版权所有</span>
      </el-footer>
    </el-container>
  </el-layout>
</template>

<style scoped>
/* 未登录状态下的容器样式 */
.auth-container {
  width: 100%;
  height: 100vh;
}

/* 全局布局 */
.app-container {
  height: 100vh;
  background-color: #f5f7fa;
  display: flex;
  overflow: hidden;
}

/* 左侧侧边栏样式 */
.aside-container {
  background-color: #2c3e50;
  transition: all 0.3s ease;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 1001;
  width: 240px;
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #1a252f;
  border-bottom: 1px solid #34495e;
}

.logo-text {
  color: #ecf0f1;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.sidebar-menu {
  border-right: none;
  height: calc(100% - 60px);
  overflow-y: auto;
}

/* 右侧主区域样式 */
.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  margin-left: 240px;
  height: 60px;
  width: calc(100% - 240px);
}

.header-left {
  display: flex;
  align-items: center;
}

.user-info {
  color: #303133;
  font-size: 14px;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-avatar {
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.05);
}

.avatar {
  background-color: #3498db;
  font-size: 20px;
}

/* 核心展示区域样式 */
.main-container {
  /* padding: 20px; */
  overflow: auto;
  background-color: #f5f7fa;
  margin-left: 240px;
  /* margin-top: 60px; */
  height: calc(100vh - 120px);
  width: calc(100% - 240px);
}

/* 底部版权信息样式 */
.footer-container {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #ffffff;
  color: #909399;
  font-size: 12px;
  border-top: 1px solid #ebeef5;
  margin-left: 240px;
  height: 60px;
  width: calc(100% - 240px);
}

/* 右侧整体布局 */
:deep(.el-layout > .el-layout) {
  display: flex;
  flex-direction: column;
  height: 100vh;
  margin-left: 240px;
  width: calc(100% - 240px);
}

/* 菜单动画效果 */
:deep(.el-menu-item) {
  transition: all 0.3s ease;
}

:deep(.el-menu-item:hover) {
  background-color: #34495e !important;
  transform: translateX(5px);
}

:deep(.el-menu-item.is-active) {
  background-color: #3498db !important;
  color: #ffffff !important;
}

/* 下拉菜单动画效果 */
:deep(.el-dropdown-menu) {
  animation: dropdownAnimation 0.3s ease;
}

@keyframes dropdownAnimation {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .aside-container {
    width: 80px;
  }
  
  .logo-text {
    font-size: 12px;
  }
  
  :deep(.el-menu-item .el-icon) {
    margin-right: 0;
  }
  
  :deep(.el-menu-item span) {
    display: none;
  }
}
</style>
